<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="prc.*" %>  
<jsp:include page="top.jsp"/>



<div id="page-body">

	
	
	<div class="panel">
		<div class="inner"><span class="corners-top"><span></span></span>
		<h2>Forum - Account Management</h2>
		<fieldset class="fields2">
		<%
			String error="";
			String oldPassword = request.getParameter("old_password");
			String password = request.getParameter("password");
			String confirmPassword = request.getParameter("password_confirm");
			String firstname = request.getParameter("firstname");
			String lastname = request.getParameter("lastname");
			String country = request.getParameter("country");
			String location = request.getParameter("location");
			String statusMsg = request.getParameter("satus_msg");
			int age=0;
			
			
			session.setAttribute( "firstname", firstname);
			session.setAttribute( "lastname", lastname);
			session.setAttribute("country",country);
			session.setAttribute("location",location);
			session.setAttribute("status_msg",statusMsg);
			
			if(request.getParameter("age").length()!=0)
				try{
					age=Integer.parseInt(request.getParameter("age"));
					session.setAttribute( "age", age);
				}catch (Exception e){
					error+="<dl><dd class=\"error\">Age must be a number</dd></dl>";
				}
			User currUsr=UserManagement.getCurrentUser(session);
			if(age!=0)
				currUsr.setAge(age);
			if(firstname.length()!=0)
				currUsr.setFirstname(firstname);
			if(lastname.length()!=0)
				currUsr.setLastname(lastname);
			if(country.length()!=0)
				currUsr.setCountry(country);
			if(location.length()!=0)
				currUsr.setLocation(location);
			currUsr.setStatusMsg(statusMsg);
			
			String username=currUsr.getUsername();
			if(password.length()!=0){
				if(oldPassword.equals(currUsr.getPassword()))
				{
					if(password.equals(confirmPassword)){
						if(password.length()<6){
							error+="<dl><dd class=\"error\">Password must have more then 6 characters</dd></dl> ";
						}
						else{
							currUsr.setPassword(password);
						}
					}
					else 
						error+="<dl><dd class=\"error\">Passwords don't match</dd></dl> ";
				}
				else
					error+="<dl><dd class=\"error\">Old Password is not correct </dd></dl> ";
			}
							
			
				
			if(error.length()>0){
				out.println(error);
				 out.println("<a href=\"userManagement.jsp\" class=\"forumtitle\" >Go Back</a>");
			}
			else{
				out.println("Success! <p><a href=\"index.jsp\" class=\"forumtitle\" >Go to index</a>");
				session.setAttribute( "username", "");
				session.setAttribute( "firstname", "");
				session.setAttribute( "lastname", "");
			}
		%>
		
	
		</fieldset>
	</div>
	<div class="panel">
		<div class="inner"><span class="corners-top"><span></span></span>
	
		<span class="corners-bottom"><span></span></span></div>
	</div>


</div>

<jsp:include page="bottom.jsp"/>